<?php

header('content-type:text/html; charset=utf-8');
// 开启Session
session_start();

// 首先判断Cookie是否有记住了用户信息
if (isset($_COOKIE['username'])) {
    # 若记住了用户信息,则直接传给Session
    $_SESSION['username'] = $_COOKIE['username'];
    $_SESSION['islogin'] = 1;
}
if (isset($_SESSION['islogin'])) {
    // 若已经登录
    echo "你好! ".$_SESSION['username'].' ,欢迎来到个人中心!<br>';
    echo "<a href='logout.php'>注销</a>";
    echo "<br>";
    echo "<br>";
    sqlCheck();

} else {
    // 若没有登录
    echo "您还没有登录,请<a href='login.html'>登录</a>";
}

//从数据库读取用户信息
function sqlCheck() {
    //数据库连接
    $mysqli = new mysqli("localhost", "root", "gcy888999", "blog");
    $mysqli->query("SET NAMES utf8");

   

    //检测连接
    if(mysqli_connect_error()) {
        die("连接失败: " . $mysqli->connect_error);
    }

    $sql = "SELECT * FROM user";
    $result = $mysqli->query($sql);

    if($result->num_rows > 0) {
        echo "所有注册用户列表:";
        echo "<br>";
        echo "<br>";
        echo "<table border='1'>
              <tr>
              <th>用户ID</th>
              <th>用户名</th>
              <th>用户密码</th>
              </tr>";
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>". $row['id']."</td>";
            echo "<td>". $row['username']."</td>";
            echo "<td>". $row['password']."</td>";
            echo "</tr>";
        }
        echo "</table>";
    }else {
        echo "从数据库读取所有用户列表失败!";
    }

}